package com.joinway.framework.data.jdbc.repository

import groovy.sql.Sql

import org.junit.Test

class JydTableRepositoryTest {
	
	final static String DB = "jiayidababy"
//	final static String DB = "jiayidababy"
	final static String TABLE_PREFIX = "jydbaby"
	
//	Sql client = Sql.newInstance("jdbc:mysql://106.186.31.59:3306/jiayidaagri?autoReconnect=true&useUnicode=true&characterEncoding=utf-8", 'jiayida', 'group123', 'com.mysql.jdbc.Driver');
//	Sql client = Sql.newInstance("jdbc:mysql://localhost:3306?autoReconnect=true&useUnicode=true&characterEncoding=utf-8", 'jiayida', 'group123', 'com.mysql.jdbc.Driver');
	Sql client = Sql.newInstance("jdbc:mysql://localhost:3306/$DB?autoReconnect=true&useUnicode=true&characterEncoding=utf-8", 'jiayida', 'group123', 'com.mysql.jdbc.Driver');
	TableRepository repo = new TableRepository(client)
	
	@Test public void test1(){
		List list = repo.find("select * from ${TABLE_PREFIX}_goods where goods_id = ?", Good.class, 17)
		println list
	}
	
	@Test public void test2(){
		List list = repo.find("select * from ${TABLE_PREFIX}_goods where goods_id = :id", [id:17], Good.class)
		println list
	}

	@Test public void test3(){
		List list = repo.find("select * from ${TABLE_PREFIX}_goods order by goods_id", Good.class, 1, 2)
		println list
	}
	
	@Test public void test4(){
		String sql = """
		SELECT DISTINCT 
			BGA.goods_attr_id attrId
			, BA.attr_name name
			, BGA.attr_value value
			FROM ${TABLE_PREFIX}_attribute BA
				LEFT JOIN ${TABLE_PREFIX}_goods_attr BGA ON BA.attr_id = BGA.attr_id
				RIGHT JOIN ${TABLE_PREFIX}_goods BG ON BGA.goods_id = BG.goods_id
		ORDER BY BGA.attr_id	
"""
		List list = repo.find(sql, GoodProperty.class)
		println list
	}
	
	@Test public void test5(){
		println repo.findOne(1, Good.class)
	}

	@Test public void test6(){
		repo.save(new Good())
	}

}
